package org.qf;
import java.sql.*;
/**
 * JDBC的使用： java连接数据库进行查询操作
 */
public class JdbcTest {
    public static void main(String[] args) {
    //1. 导入依赖jar mySQL
    //2. 加载 驱动
    // 数据库5版本: com.mysql.jdbc.Driver
    // 8版本：com.mysql.cj.jdbc.Driver
        try {
            // alt+enter try..catch
            Class.forName("com.mysql.cj.jdbc.Driver");
            //3.创建连接
            // 数据库8 加入serverTimezone=UTC时区
            //jdbc:mysql://localhost:3306/mydb?characterEncoding=utf-8&serverTimezone=UTC
            // 数据库5 jdbc:mysql://localhost:3306/mydb?characterEncoding=utf-8
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/day?characterEncoding=utf-8&serverTimezone=UTC", "root", "1234");
            //4.写sql
            String sql="select * from day_user";
            //5. 创建PreparedStatement对象
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            //6.执行 命令
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                System.out.println(resultSet.getInt(1)+"\t"+resultSet.getString(2)+"\t"+resultSet.getInt(3));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}